﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Mailer.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains Mailer class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IBupro.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Resources;
    using System.Web;
    using System.Xml.Linq;
    using iBuproModel;
    using System.Text;

    /// <summary>
    /// Mailer sends messages via e-mail to users.
    /// </summary>
    public class Mailer : IMailer
    {

        public void ProjectHasBeenBooked(DiplomProject project, User student)
        {
            string message = LoadMessageFromXml("bookedProjectNotification");
            message = message.Replace("#project#", project.Name);
            message = message.Replace("#student#", student.FirstName + " " + student.LastName);
            message = message.Replace("#studentEmail#",student.Email);
            this.MailTo(project.Lector.Email, "iBupro - Projekt", message);
        }

        public void MailTo(string destinationEmail, string subject, string message)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["fromEmail"], destinationEmail);
            msg.Subject = subject;
            msg.Body = message;
            msg.HeadersEncoding = Encoding.GetEncoding("UTF-8");
            msg.BodyEncoding = Encoding.GetEncoding("UTF-8");

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = System.Configuration.ConfigurationManager.AppSettings["mailServer"];
            smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["emailUsername"], System.Configuration.ConfigurationManager.AppSettings["emailPassword"]);

            try
            {
                if (System.Configuration.ConfigurationManager.AppSettings["sendingMailsEnabled"].ToLower() == "true")
                    smtp.Send(msg);
            }
            catch
            {
                return;
            }
            

        }

        private string LoadMessageFromXml(string key)
        {
            XDocument doc = XDocument.Load(
                   HttpContext.Current.Server.MapPath(
                      "~/App_GlobalResources/emails.xml"));

            //return doc.Elements("").Where(e => e.Attribute("key").ToString() == "bookedProjectNotification").First().Value;
            string message = doc.Element("email").Descendants("message").First(e => e.Attribute("key").Value == key).Value;
            return message;
           
        }



        public void ProjectHasBeenAcceptedByLector(DiplomProject project, string customMessage)
        {
            string message = LoadMessageFromXml("principalApprovalNotification");
            message = message.Replace("#project#", project.Name);
            message = message.Replace("#message#", customMessage);
            this.MailTo(project.Principal.Email, "iBupro - Projekt", message);
        }


        public void ProjectHasBeenAcceptedByPrincipal(DiplomProject project)
        {
            string message = LoadMessageFromXml("principalHasAcceptedProjectNotification");
            message = message.Replace("#project#", project.Name);
            this.MailTo(project.Student.Email, "iBupro - Projekt", message);
            this.MailTo(project.Lector.Email, "iBupro - Projekt", message);

        }

        public void ProjectHasBeenDeniedByPrincipal(DiplomProject project, string customMessage)
        {
            string message = LoadMessageFromXml("principalHasDeniedProjectNotification");
            message = message.Replace("#project#", project.Name);
            message = message.Replace("#message#", customMessage);
            this.MailTo(project.Lector.Email, "iBupro - Projekt", message);
        }


        public void StudentRequestStudentProjectToLector(Project project, User requester)
        {
            string message = LoadMessageFromXml("studentRequestStudentProjectToLector");
            message = message.Replace("#project#", project.Name);
            message = message.Replace("#user#", requester.FirstName + " " + requester.LastName);
            this.MailTo(project.Principal.Email, "iBupro - Projekt", message);
        }

        public void LectorAcceptReplyStudentProjectRequest(Project project, User requester)
        {
            string message = LoadMessageFromXml("lectorAcceptReplyStudentProjectRequest");
            message = message.Replace("#project#", project.Name);
            this.MailTo(requester.Email, "iBupro - Projekt", message);
        }

        public void LectorDenyReplyStudentProjectRequest(Project project, User requester)
        {
            string message = LoadMessageFromXml("lectorDenyReplyStudentProjectRequest");
            message = message.Replace("#project#", project.Name);
            this.MailTo(requester.Email, "iBupro - Projekt", message);
        }
    }
}